* Master do-file for Harada and Smith, "Distributive Politics and Crime" 

************************
* INITIAL SETTINGS
************************
clear all
set more off


************************************************
* REQUIRED PACKAGES TO RUN THE FOLLOWING CODES
************************************************
ssc install avar
ssc install weakiv
ssc install ivreg2
ssc install ranktest
ssc install xtivreg2


************************************************
* TABLE A.1: TYPES OF REPORTED CRIME
************************************************

* DOES NOT INVOLVE ANALYSIS. SEE TABLE FOR SOURCE.

************************************************
* TABLE A.2: DESCRIPTIVE STATISTICS
************************************************
use MASTER, clear
xtset muncode1998B year
keep if d_city

tabstat lnCrimePC lnr_unemp_t lnr_unemp_m lnr_unemp_f lnr_Shotoku lnLATPC lnMal lnPop r_Young_pop r_Old_pop lnpop_density if sample9697 == 1 & (year == 1996 | year == 1997), stat(n mean sd min max) col(stat)


************************************************
* TABLE 1: ANALYSIS OF CRIME SUBCATEGORIES
************************************************
use MASTER_TABLE1, clear
xtset code FY

xi: xtreg K4201 i.FY if FY == 1996 | FY == 1997, fe cluster(code )
xi: xtreg K4201 K420103 i.FY if FY == 1996 | FY == 1997, fe cluster(code )
xi: xtreg K4201 K42010* i.FY if FY == 1996 | FY == 1997, fe cluster(code )


************************************************
* TABLE 2: 1ST STAGE RESULTS
* TABLE A.3: 1ST STAGE RESULTS (COMPLETE RESULTS)
************************************************
use MASTER, clear
xtset muncode1998B year
global ctrl ="lnPop r_Young_pop r_Old_pop lnpop_density"
keep if d_city

* Without Controls
xtivreg2 lnCrimePC d_yr_1997 (lnLATPC = lnMal) if sample9697 & (year == 1996 | year == 1997), fe cluster(SMD_dist year) first savefirst

* With Controls
xtivreg2 lnCrimePC d_yr_1997 $ctrl (lnLATPC = lnMal) if sample9697 & (year == 1996 | year == 1997), fe cluster(SMD_dist year) first savefirst

* 1ST STAGE REGRESSION TO COMPUTE R-SQ
xtreg lnLATPC lnMal d_yr_1997 if sample9697 & (year == 1996 | year == 1997), fe cluster(SMD_dist)
xtreg lnLATPC lnMal d_yr_1997 $ctrl if sample9697 & (year == 1996 | year == 1997), fe cluster(SMD_dist) 


************************************************
* TABLE A.4: CHECKING TREND EFFECTS
************************************************
use MASTER, clear
xtset muncode1998B year
global ctrl ="lnPop r_Young_pop r_Old_pop lnpop_density"
keep if d_city

xtivreg2 lnCrimePC d_yr_1996 $ctrl (lnLATPC = lnMal) if (year == 1996 | year == 1995), fe cluster(SMD_dist year) first
weakiv, md level(95) gridpoints(2000)


************************************************
* TABLE 3: 2ND STAGE RESULTS
* TABLE A.5: 2ND STAGE RESULTS (COMPLETE RESULTS)
************************************************
use MASTER, clear
xtset muncode1998B year
global ctrl ="lnPop r_Young_pop r_Old_pop lnpop_density"
keep if d_city

* (1) NAIVE W/O CTRL
xtivreg2 lnCrimePC lnLATPC d_yr_1997 if sample9697 & (year == 1996 | year == 1997), fe cluster(SMD_dist year)

* (2) NAIVE W/ CTRL
xtivreg2 lnCrimePC lnLATPC d_yr_1997 $ctrl if sample9697 & (year == 1996 | year == 1997), fe cluster(SMD_dist year)

* (3) IV W/O CTRL
xtivreg2 lnCrimePC d_yr_1997 (lnLATPC = lnMal) if sample9697 & (year == 1996 | year == 1997), fe cluster(SMD_dist year)
weakiv, md level(95) gridpoints(2000)

* (4) IV W/ CTRL
xtivreg2 lnCrimePC d_yr_1997 $ctrl (lnLATPC = lnMal) if sample9697 & (year == 1996 | year == 1997), fe cluster(SMD_dist year)
weakiv, md level(95) gridpoints(2000)


* CALCULATING SUBSTANTIVE SIGNIFICANCE
use MASTER, clear
xtset muncode1998B year
global ctrl ="lnPop r_Young_pop r_Old_pop lnpop_density"
keep if d_city

foreach v in Pop LAT Crime {
	sum `v' if sample9697 == 1
	local mean_`v' = r(mean)
}
local usd = 110

* AVERAGE POPULATION OF MUNICIPALITIES
di `mean_Pop'

* AVERAGE SUBSIDY OF MUNICIPALITIES
di `mean_LAT'*1000000
di `mean_LAT'*1000000/`usd'

* AVERAGE SUBSIDY PER CAPITA
di `mean_LAT'*1000000/`mean_Pop'
di `mean_LAT'*1000000/`mean_Pop'/`usd'

* AVERAGE CRIME IN MUNICIPALITIES
di `mean_Crime'

* 1% OF AVERAGE SUBSIDY IN A MUNICIPALITY WITH AVERAGE POPULATION
di 0.01*(`mean_LAT'*1000000)
di 0.01*(`mean_LAT'*1000000)/`usd'

* NUMBER OF CRIMES REDUCED BY 1 P.P. INCREASE IN LAT
di 2352.821*.01*0.249

* CALCULATE COST OF PREVENTING SINGLE CRIME
di (0.01*(`mean_LAT'*1000000))/(2352.821*.01*0.249)
di (0.01*(`mean_LAT'*1000000/`usd'))/(2352.821*.01*0.249)


**************************************
* CALCULATING SUBSTANTIVE SIGNIFICANCE
**************************************
use MASTER, clear
xtset muncode1998B year
global ctrl ="lnPop r_Young_pop r_Old_pop lnpop_density"
keep if d_city

* CALCULATE AVERAGE POP, LAT, AND UNEMPLOYMENT RATES
local usd = 110
foreach v in Pop LAT r_unemp_t {
	sum `v' if sample9697 == 1
	local mean_`v' = r(mean)
}
* reduced unemp in p.p. by 1 percent increase in transfers
di `mean_r_unemp_t' * .00067 * 100 /*percent*/

* average unemp per city
di `mean_Pop' * `mean_r_unemp_t'

* number of unemp person saved by 1% increase in transfers
di `mean_Pop' * `mean_r_unemp_t' * .00067

* 1% OF AVERAGE SUBSIDY IN A CITY WITH AVERAGE POPULATION
di 0.01*(`mean_LAT'*1000000)

* Cost of transfers needed to save 1 unemp person
di (0.01*(`mean_LAT'*1000000)) / (`mean_Pop' * `mean_r_unemp_t' * .00067)
di (0.01*(`mean_LAT'*1000000)) / (`mean_Pop' * `mean_r_unemp_t' * .00067) / `usd'


************************************************
* TABLE 4: ROBUSTNESS CHECKS
************************************************
* RC1: PLACEBO TEST
* TABLE A.6: COMPLETE RESULTS
* CHANGE DV FROM 96-97 TO 95-96
* lnCrimePC --> lnCrimePC_l1
use MASTER, clear
xtset muncode1998B year
global ctrl ="lnPop r_Young_pop r_Old_pop lnpop_density"
keep if d_city

xtivreg2 lnCrimePC_l1 d_yr_1997 $ctrl (lnLATPC = lnMal) if sample9697 & (year == 1996 | year == 1997), fe cluster(SMD_dist year) first savefirst
weakiv, md level(95) gridpoints(2000)

* RC2: Extended Control 
* TABLE A.7: COMPLETE RESULTS
use MASTER, clear
xtset muncode1998B year
global extctrl ="lnPop r_Young_pop r_Old_pop lnpop_density r_Sec1_pop r_Sec3_pop r_DID_pop Zaiseiryoku dm lnLJStotcwins LJScabexp"
keep if d_city

xtivreg2 lnCrimePC d_yr_1996 $extctrl (lnLATPC = lnMal) if sample9697 & (year == 1996 | year == 1997), fe cluster(SMD_dist year) first savefirst
weakiv, md level(95) gridpoints(2000)

* RC3: USING 2 IVs (w/ battle ground)
use MASTER, clear
xtset muncode1998B year
global ctrl ="lnPop r_Young_pop r_Old_pop lnpop_density"
keep if d_city & sample9697

order flg_bg010 HOR SMD_dist
*COUNT # OF BATTLEGROUND DISTRICT in 1996
gsort -flg_bg010 year HOR muncode1998B

*COUNT # OF BATTLEGROUND DISTRICT in 1997
gsort -flg_bg010 -year SMD_dist muncode1998B

*COUNT # OF BATTLEGROUND CITIES in 1996
gsort -flg_bg010 year HOR muncode1998B

*COUNT # OF BATTLEGROUND CITIES in 1997
gsort -flg_bg010 -year SMD_dist muncode1998B

use MASTER, clear
global ctrl ="lnPop r_Young_pop r_Old_pop lnpop_density"
keep if d_city

* BG THRESHOLD = 1%
xtivreg2 lnCrimePC d_yr_1997 $ctrl (lnLATPC = lnMal flg_bg010) if sample9697 & (year == 1996 | year == 1997), cue fe cluster(SMD_dist year) first savefirst
weakiv, md level(95) gridpoints(2000)


************************************************
* USING TWO IVs W/ DIFFERENT THRESHOLDS FOR BATTLEGROUND(BG)
* TABLE A.8: FIRST STAGE
* TABLE A.9: SECOND STAGE
************************************************
use MASTER, clear
xtset muncode1998B year
global ctrl ="lnPop r_Young_pop r_Old_pop lnpop_density"
keep if d_city

* BG THRESHOLD = 0.5%
xtivreg2 lnCrimePC d_yr_1997 $ctrl (lnLATPC = lnMal flg_bg) if sample9697 & (year == 1996 | year == 1997), cue fe cluster(SMD_dist year) first savefirst
weakiv, md level(95) gridpoints(2000)
qui xtivreg2 lnCrimePC d_yr_1997 $ctrl (lnLATPC = lnMal flg_bg) if sample9697 & (year == 1996 | year == 1997), cue fe cluster(SMD_dist year) first savefirst
weakiv, md level(90) gridpoints(2000)

* BG THRESHOLD = 1%
xtivreg2 lnCrimePC d_yr_1997 $ctrl (lnLATPC = lnMal flg_bg010) if sample9697 & (year == 1996 | year == 1997), cue fe cluster(SMD_dist year) first savefirst
weakiv, md level(95) gridpoints(2000)
qui xtivreg2 lnCrimePC d_yr_1997 $ctrl (lnLATPC = lnMal flg_bg010) if sample9697 & (year == 1996 | year == 1997), cue fe cluster(SMD_dist year) first savefirst
weakiv, md level(90) gridpoints(2000)

* BG THRESHOLD = 2%
xtivreg2 lnCrimePC d_yr_1997 $ctrl (lnLATPC = lnMal flg_bg020) if sample9697 & (year == 1996 | year == 1997), cue fe cluster(SMD_dist year) first savefirst
weakiv, md level(95) gridpoints(2000)
qui xtivreg2 lnCrimePC d_yr_1997 $ctrl (lnLATPC = lnMal flg_bg020) if sample9697 & (year == 1996 | year == 1997), cue fe cluster(SMD_dist year) first savefirst
weakiv, md level(90) gridpoints(2000)


************************************************
* TABLE A.10 MODEL(2)
* RC4: EXCLUDE YAKUZA HQ
* Kobe, Minato-ku, Kitakyushu, Naha, Kyoto, 
* Hiroshima, Shimonoseki, Kagoshima, Kasaoka, 
* Kurume, Tkamatsu, Ichihara, Onomichi, 
* Tagawa, Toshima-ku, Osaka, and Taito-ku
************************************************
use MASTER, clear
xtset muncode1998B year
global ctrl ="lnPop r_Young_pop r_Old_pop lnpop_density"
keep if d_city

* CREATE YAKUZA HQ DUMMY
gen yakuzaHQ = 0
replace yakuzaHQ = 1 if muncode1998B==28100
replace yakuzaHQ = 1 if muncode1998B==13103
replace yakuzaHQ = 1 if muncode1998B==40100
replace yakuzaHQ = 1 if muncode1998B==47201
replace yakuzaHQ = 1 if muncode1998B==26100
replace yakuzaHQ = 1 if muncode1998B==34100
replace yakuzaHQ = 1 if muncode1998B==35201
replace yakuzaHQ = 1 if muncode1998B==46201
replace yakuzaHQ = 1 if muncode1998B==33205
replace yakuzaHQ = 1 if muncode1998B==40203
replace yakuzaHQ = 1 if muncode1998B==37201
replace yakuzaHQ = 1 if muncode1998B==12219
replace yakuzaHQ = 1 if muncode1998B==34205
replace yakuzaHQ = 1 if muncode1998B==40206
replace yakuzaHQ = 1 if muncode1998B==13116
replace yakuzaHQ = 1 if muncode1998B==27100
replace yakuzaHQ = 1 if muncode1998B==13106

xtivreg2 lnCrimePC d_yr_1997 $ctrl (lnLATPC = lnMal) if yakuzaHQ==0 & sample9697 & (year == 1996 | year == 1997), fe first cluster(SMD_dist year)
weakiv, md level(95) gridpoints(2000)


************************************************
* TABLE A.10 MODEL(3)
* RC5: EXCLUDE CITIES WITH LOCAL ELECTION BTW 96-97
* Itoman, Kushiro, Ohnojo, Hikone, Kamifukuoka,
* Komae, Ohtsu, Nakatsugawa, Komoro
************************************************
use MASTER, clear
xtset muncode1998B year
global ctrl ="lnPop r_Young_pop r_Old_pop lnpop_density"
keep if d_city

* CREATE LOCAL ELECTION DUMMY
gen localelec = 0
replace localelec = 1 if muncode1998B==47210
replace localelec = 1 if muncode1998B==1206
replace localelec = 1 if muncode1998B==40219
replace localelec = 1 if muncode1998B==25202
replace localelec = 1 if muncode1998B==11236
replace localelec = 1 if muncode1998B==13219
replace localelec = 1 if muncode1998B==25201
replace localelec = 1 if muncode1998B==21206
replace localelec = 1 if muncode1998B==20208

* ESTIMATION MODEL
xtivreg2 lnCrimePC d_yr_1997 $ctrl (lnLATPC = lnMal) if localelec==0 & sample9697 & (year == 1996 | year == 1997), fe first cluster(SMD_dist year)
weakiv, md level(95) gridpoints(2000)


************************
* TABLE A.11 
* RC6: USING ALL MUNICIPALITIES
************************
use MASTER, clear
xtset muncode1998B year
global ctrl ="lnPop r_Young_pop r_Old_pop lnpop_density"

xtivreg2 lnCrimePC d_yr_1996 $ctrl (lnLATPC = lnMal) if sample9697 & (year == 1996 | year == 1997), fe cluster(SMD_dist year) first savefirst
weakiv, md level(95) gridpoints(2000)


***************************************
* TABLE 5: VARIOUS DEPENDENT VARIABLES
* TABLE A.12: FIRST STAGE
* TABLE A.13: SECOND STAGE
***************************************
use MASTER, clear
xtset muncode1998B year
global ctrl ="lnPop r_Young_pop r_Old_pop lnpop_density"
keep if d_city

* Unemployment Rate (Total) OLS
xtivreg2 lnr_unemp_t lnLATPC d_yr_1997 $ctrl if sample9697, fe cluster(SMD_dist year)

* Unemployment Rate (Total) IV estimate
xtivreg2 lnr_unemp_t d_yr_1997 $ctrl (lnLATPC = lnMal) if sample9697, fe cluster(SMD_dist year)
weakiv, md level(95) gridpoints(2000)

* Unemployment Rate (Male) OLS
xtivreg2 lnr_unemp_m lnLATPC d_yr_1997 $ctrl if sample9697, fe cluster(SMD_dist year)

* Unemployment Rate (Male) IV estimate
xtivreg2 lnr_unemp_m d_yr_1997 $ctrl (lnLATPC = lnMal) if sample9697, fe cluster(SMD_dist year)
weakiv, md level(95) gridpoints(2000)

* Unemployment Rate (Female) OLS
xtivreg2 lnr_unemp_f lnLATPC d_yr_1997 $ctrl if sample9697, fe cluster(SMD_dist year)

* Unemployment Rate (Female) IV estimate
xtivreg2 lnr_unemp_f d_yr_1997 $ctrl (lnLATPC = lnMal) if sample9697, fe cluster(SMD_dist year)
weakiv, md level(95) gridpoints(2000)

* Taxable Income Per Capita OLS
xtivreg2 lnr_Shotoku lnLATPC d_yr_1997 $ctrl if sample9697, fe cluster(SMD_dist year)

* Taxable Income Per Capita IV estimate
xtivreg2 lnr_Shotoku d_yr_1997 $ctrl (lnLATPC = lnMal) if sample9697, fe cluster(SMD_dist year)
weakiv, md level(95) gridpoints(2000)



**************************************
* FIGURE A.1: BALANCE CHECK
**************************************
use MASTER, clear
xtset muncode1998B year
global ctrl ="lnPop r_Young_pop r_Old_pop lnpop_density"
keep if d_city


* drop unused obs
qui xtivreg2 lnCrimePC d_yr_1997 $ctrl (lnLATPC = lnMal) if sample9697 & (year == 1996 | year == 1997), fe cluster(SMD_dist year) first savefirst
keep if e(sample)


*CREATE FIRST-DIFFERENCED VARIABLES
sort muncode1998B year
foreach var in lnMal $ctrl {
	gen df_`var' = `var' - l.`var'
	egen zdf_`var' = std(df_`var')
}

*CREATE QUANTILE MALAPPORTIONMENT VARIABLE
gen q1_df_lnMal = 0
gen q2_df_lnMal = 0
gen q3_df_lnMal = 0
gen q4_df_lnMal = 0

quietly sum zdf_lnMal if year==1997, d

replace q1_df_lnMal = 1 if sample9697 & year == 1997 & zdf_lnMal <= r(p25)
replace q2_df_lnMal = 1 if sample9697 & year == 1997 & zdf_lnMal > r(p25) & zdf_lnMal <= r(p50)
replace q3_df_lnMal = 1 if sample9697 & year == 1997 & zdf_lnMal > r(p50) & zdf_lnMal <= r(p75)
replace q4_df_lnMal = 1 if sample9697 & year == 1997 & zdf_lnMal > r(p75)

forval q = 1/4 {
	replace q`q'_df_lnMal = q`q'_df_lnMal[_n+1] if year == 1996 & muncode1998B==muncode1998B[_n+1] & year[_n+1]==1997
}


* PRODUCE FOUR KERNEL DENSITY PLOTS FOR EACH COVARIATE
* BALANCE FOR POPULATION
twoway ///
(kdensity zdf_lnPop if q1_df_lnMal == 1, lcolor(midgreen) lpattern(shortdash) lwidth(medthick)) ///
(kdensity zdf_lnPop if q2_df_lnMal == 1, lcolor(gold) lpattern(dash) lwidth(medthick)) ///
(kdensity zdf_lnPop if q3_df_lnMal == 1, lcolor(orange) lpattern(longdash) lwidth(medthick)) ///
(kdensity zdf_lnPop if q4_df_lnMal == 1, lcolor(red) lpattern(solid) lwidth(medthick)), ///
ylabel(, nogrid) yscale(off) ///
xtitle(1st Diff. Population (log)) ///
xtitle(, size(large)) ///
xlabel(, labsize(large)) ///
legend(off) xsize(1) ysize(1) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
saving(zdf_lnPop,replace)

* BALANCE FOR YOUNG POPULATION
twoway ///
(kdensity zdf_r_Young_pop if q1_df_lnMal == 1, lcolor(midgreen) lpattern(shortdash) lwidth(medthick)) ///
(kdensity zdf_r_Young_pop if q2_df_lnMal == 1, lcolor(gold) lpattern(dash) lwidth(medthick)) ///
(kdensity zdf_r_Young_pop if q3_df_lnMal == 1, lcolor(orange) lpattern(longdash) lwidth(medthick)) ///
(kdensity zdf_r_Young_pop if q4_df_lnMal == 1, lcolor(red) lpattern(solid) lwidth(medthick)), ///
ylabel(, nogrid) yscale(off) ///
xtitle(1st Diff. Pop. < 15) ///
xtitle(, size(large)) ///
xlabel(, labsize(large)) ///
legend(off) xsize(1) ysize(1) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
saving(zdf_r_Young_pop,replace)

* BALANCE FOR OLD POPULATION
twoway ///
(kdensity zdf_r_Old_pop if q1_df_lnMal == 1, lcolor(midgreen) lpattern(shortdash) lwidth(medthick)) ///
(kdensity zdf_r_Old_pop if q2_df_lnMal == 1, lcolor(gold) lpattern(dash) lwidth(medthick)) ///
(kdensity zdf_r_Old_pop if q3_df_lnMal == 1, lcolor(orange) lpattern(longdash) lwidth(medthick)) ///
(kdensity zdf_r_Old_pop if q4_df_lnMal == 1, lcolor(red) lpattern(solid) lwidth(medthick)), ///
ylabel(, nogrid) yscale(off) ///
xtitle(1st Diff. Pop. > 65) ///
xtitle(, size(large)) ///
xlabel(, labsize(large)) ///
legend(off) xsize(1) ysize(1) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
saving(zdf_r_Old_pop,replace)

* BALANCE FOR POPULATION DENSITY
twoway ///
(kdensity zdf_lnpop_density if q1_df_lnMal == 1, lcolor(midgreen) lpattern(shortdash) lwidth(medthick)) ///
(kdensity zdf_lnpop_density if q2_df_lnMal == 1, lcolor(gold) lpattern(dash) lwidth(medthick)) ///
(kdensity zdf_lnpop_density if q3_df_lnMal == 1, lcolor(orange) lpattern(longdash) lwidth(medthick)) ///
(kdensity zdf_lnpop_density if q4_df_lnMal == 1, lcolor(red) lpattern(solid) lwidth(medthick)), ///
ylabel(, nogrid) yscale(off) ///
xtitle(1st Diff. Pop. Density(log)) ///
xtitle(, size(large)) ///
xlabel(, labsize(large)) ///
legend(off) xsize(1) ysize(1) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
saving(zdf_lnpop_density,replace)

*COMBINE THE FIGURES
graph combine zdf_lnPop.gph zdf_r_Young_pop.gph zdf_r_Old_pop.gph zdf_lnpop_density.gph, ///
rows(1) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
imargin(0 0 0 0) graphregion(margin(l=12 r=12)) ///
ysize(1) xsize(4) iscale(*2)  

*SAVE THE COMBINED FIGURE
graph export balance_check_quantile.pdf, as(pdf) replace


**************************************
* FIGURE 1: NONPARAMETERIC LOCAL REGRESSION
**************************************
* NOTE ON KERNEL SPECIFICATION
* BY DEFAULT, STATA17 PRODUCES lpolyci FIGURE USING kernel(epan2) OPTION. HOWEVER, STATA17 USES THIS OPTION WHETHER WE SPECIFY kernel(epanechnikov) OR kernel(epan2). SINCE WE DO NOT KNOW WHY STATA DISCONTIUNUED kernel(epanechnikov), WE AVOID AMBIGUITY BY EXPLICITLY SPECIFYING kernel(epan2) IN THE FOLLOWING CODES.

use MASTER,clear
xtset muncode1998B year
global ctrl ="lnPop r_Young_pop r_Old_pop lnpop_density"
keep if d_city

* RESIDUALIZE lnLATPC
areg lnLATPC $ctrl d_yr_1997 if (year == 1996 | year == 1997), absorb(muncode1998B)
predict rx1, resid

* RESIDUALIZE lnMal
areg lnMal $ctrl d_yr_1997 if (year == 1996 | year == 1997), absorb(muncode1998B)
predict rz1, resid

* RESIDUALIZE lnCrimePC
areg lnCrimePC lnLATPC $ctrl d_yr_1997 if (year == 1996 | year == 1997), absorb(muncode1998B)
predict ry2, resid

* RESIDUALIZE lnMal
areg lnMal lnLATPC $ctrl d_yr_1997 if (year == 1996 | year == 1997), absorb(muncode1998B)
predict rz2, resid

* PRODUCE THE LEFT PANEL
twoway (lpolyci rx1 rz1, kernel(epan2) clcolor(midblue) clwidth(medthick) fcolor(ltblue) blcolor(ltblue)), ///
ytitle(ln(Transfers per capita)[residual]) ///
ytitle(, size(large)) ///
ylabel(, labsize(large) gmin gmax) ///
xtitle(ln(Malapportionment)[residual]) ///
xtitle(, size(large)) ///
xlabel(, labsize(large)) ///
graphregion(fcolor(white) lcolor(white)) ///
plotregion(fcolor(white) lcolor(white)) ///
legend(on cols(1) order(2 "Local regression fit" 1 "95% CI") size(large) nobox region(fcolor(white) lcolor(white)) ring(0)) ///
xsize(4) ysize(5) ///
saving(localreg1, replace)

* PRODUCE THE RIGHT PANEL
twoway (lpolyci ry2 rz2, kernel(epan2) clcolor(midblue) clwidth(medthick) fcolor(ltblue) blcolor(ltblue)), ///
ytitle(ln(Crimes per 1,000 residents)[residual]) ///
ytitle(, size(large)) ///
ylabel(, labsize(large) gmin gmax) ///
xtitle(ln(Malapportionment)[residual]) ///
xtitle(, size(large)) ///
xlabel(, labsize(large)) ///
graphregion(fcolor(white) lcolor(white)) ///
plotregion(fcolor(white) lcolor(white)) ///
legend(on cols(1) order(2 "Local regression fit" 1 "95% CI") size(large) nobox region(fcolor(white) lcolor(white)) ring(0)) ///
xsize(4) ysize(5) ///
saving(localreg2, replace)

* COMBINE GRAPHS
gr combine localreg1.gph localreg2.gph, ///
note("Note: Local regression fit is created using Epanechnikov kernel.") ///
xsize(6) ysize(3) ///
graphregion(fcolor(white) lcolor(white)) ///
plotregion(fcolor(white) lcolor(white))

graph export Figure1.pdf, as(pdf) replace



